package com.millstein.educenter.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.millstein.commonutils.JwtUtils;
import com.millstein.educenter.entity.UcenterMember;
import com.millstein.educenter.entity.vo.RegisterVo;
import com.millstein.educenter.mapper.UcenterMemberMapper;
import com.millstein.educenter.service.UcenterMemberService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.millstein.educenter.utils.MD5;
import com.millstein.servicebase.handler.exceptionhandler.GuliException;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;
import org.springframework.util.StringUtils;

import java.util.List;

/**
 * <p>
 * 会员表 服务实现类
 * </p>
 *
 * @author testjava
 * @since 2020-07-26
 */
@Service
public class UcenterMemberServiceImpl extends ServiceImpl<UcenterMemberMapper, UcenterMember> implements UcenterMemberService {

    private static final String VALID_PREFIX = "valid:";

    @Autowired
    private RedisTemplate<String, String> redisTemplate;

    @Override
    public String login(UcenterMember member) {
        String mobile = member.getMobile();
        String password = member.getPassword();
        if (StringUtils.isEmpty(mobile) || StringUtils.isEmpty(password)) {
            throw new GuliException(20001, "登录信息不完整");
        }

        QueryWrapper<UcenterMember> wrapper = new QueryWrapper<>();
        wrapper.eq("mobile", mobile);
        wrapper.eq("password", MD5.encrypt(password));
        UcenterMember dbMember = this.getOne(wrapper);
        if (dbMember == null) {
            throw new GuliException(20001, "用户名或密码有误");
        }
        if (dbMember.getIsDisabled()) {
            throw new GuliException(20001, "用户已经被禁用，请联系管理员");
        }

        return JwtUtils.getJwtToken(dbMember.getId(), dbMember.getNickname());
    }

    @Override
    public void register(RegisterVo vo) {
        String mobile = vo.getMobile();
        String code = vo.getCode();

        if (StringUtils.isEmpty(mobile) ||StringUtils.isEmpty(code)) {
            throw new GuliException(20001, "注册信息不完全");
        }

        if (!redisTemplate.hasKey(VALID_PREFIX + mobile)) {
            throw new GuliException(20001, "验证码失效，请重新获取");
        }
        if (!code.equals(redisTemplate.opsForValue().get(VALID_PREFIX + mobile)))  {
            throw new GuliException(20001, "验证码有误");
        }

        QueryWrapper<UcenterMember> wrapper = new QueryWrapper<>();
        wrapper.eq("mobile", mobile);
        wrapper.eq("is_deleted", false);
        int count = this.count(wrapper);
        if (count != 0) {
            throw new GuliException(20001, "已存在相同的手机号码，不能完成注册");
        }

        UcenterMember member = new UcenterMember();
        BeanUtils.copyProperties(vo, member);
        member.setPassword(MD5.encrypt(vo.getPassword()));
        member.setIsDisabled(false);
        member.setIsDeleted(false);
        boolean save = this.save(member);
        if (!save) {
            throw new GuliException(20001, "注册失败");
        }

        // 删除redis中对应的键值对
        redisTemplate.delete(VALID_PREFIX + mobile);
    }
}
